<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
      <el-form-item label="岗位编码" prop="postCode">
        <el-input
          v-model="queryParams.postCode"
          placeholder="请输入岗位编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="">
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
    </el-row>
    <el-table ref="multipleTable" v-loading="loading" :row-key="getRowKey" :data="postList" @selection-change="handleSelectionChange" @row-click="rowChecked">
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column label="岗位编码" align="center" prop="postCode" />
      <el-table-column label="岗位名称" align="center" prop="postName" />
      <el-table-column label="岗位排序" align="center" prop="postSort" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope" :formatter="statusFormat">
          <span v-if="scope.row.status=== '0'">正常</span>
          <span v-if="scope.row.status=== '1'">停用</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
    </el-table>

    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listPost
} from '@/api/system/post'
export default {
  // 接受父组件传过来的值
  props: {
    dataScopListDelList: {
      type: Array,
      // 当为数组类型设置默认值时必须使用数组返回
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的行对象
      multipleSelection: [],
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        postName: '',
        postCode: '',
        status: ''
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response.data.rows
    })
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listPost(this.queryParams).then(response => {
        if (response) {
          this.postList = response.data.rows
          this.total = response.data.total
          this.echo()
          this.loading = false
        } else {
          this.msgError(response.message)
        }
      })
    },
    // 回显数据
    echo: function() {
      // 获取父组件中 封装自定义行和列的信息（dataScopListDelList）
      var dataScopListDelList = this.dataScopListDelList
      var dataObjRowList = []
      // 遍历数组获取封装行对象的数组
      if (dataScopListDelList != null && dataScopListDelList.length > 0) {
        for (var i = 0; i < dataScopListDelList.length; i++) {
          if (dataScopListDelList[i].dataObjRecordType === 'row') {
            dataObjRowList = dataScopListDelList[i].dataObjRowList
          }
        }
      }
      // 回显选中了那些行
      dataObjRowList.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row)
      })
    },
    // 分页记录回显数据
    getRowKey(row) {
      return row.postId
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.ids = selection.map(item => item.postId)
      this.checkIds(selection)
    },
    // 获取选中的行数并且把值返回到父组件（roleDataScopeDet）
    checkIds(val) {
      var ids = ''
      for (var i = 0; i < val.length; i++) {
        ids += val[i].postId + ','
      }
      this.$emit('multipleSelection', ids, this.multipleSelection)
    }
  }
}
</script>
